﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace OutputTree
{
    public class TreeNodeItemRoutedEventArgs : RoutedEventArgs
    {
        public TreeNodeItemContent TreeNodeContentItem { get; set; }
        public OutputItem OutputItem { get; set; }

        public TreeNodeItemRoutedEventArgs(TreeNodeItemContent item, OutputItem outputItem)
        {
            this.TreeNodeContentItem = item;
            this.OutputItem = outputItem;
        }
    }

    public partial class TreeNodeItemContent : UserControl
    {
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(TreeNodeItemContent), new PropertyMetadata(TextPropertyChanged));
        public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(TreeNodeItemContent), new PropertyMetadata(ColorPropertyChanged));
        public delegate void NodeCreatedHandler(object sender, TreeNodeItemRoutedEventArgs e);

        public TreeViewItem TreeViewItem { get; set; }
        public Button ButtonContainer { get; set; }

        public TreeNodeItemContent()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            var parent = VisualTreeHelper.GetParent(this) as FrameworkElement;

            while (!(parent is TreeViewItem))
            {
                parent = VisualTreeHelper.GetParent(parent) as FrameworkElement;

                if (parent is Button)
                {
                    this.ButtonContainer = parent as Button;
                }
            }

            var treeViewItem = parent as TreeViewItem;

            this.TreeViewItem = treeViewItem;
            var outputItem = (OutputItem)treeViewItem.GetItem();

            outputItem.TreeViewItem = treeViewItem;

            outputItem.RaiseNodeContentCreated(this);

            treeViewItem.IsExpanded = true;
        }

        private static void TextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TreeNodeItemContent)d).SetText((string) e.NewValue);
        }

        private static void ColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TreeNodeItemContent)d).SetColor((Color)e.NewValue);
        }

        private void SetColor(Color color)
        {
            NodeTextBlock.Foreground = new SolidColorBrush(color);
        }

        private void SetText(string text)
        {
            NodeTextBlock.Text = text;
        }

        public string Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }

            set
            {
                SetValue(TextProperty, value);
            }
        }

        public Color Color
        {
            get
            {
                return (Color)GetValue(ColorProperty);
            }

            set
            {
                SetValue(ColorProperty, value);
            }
        }
    }
}
